package com.ruoyi.platform.system.user.controller;

import com.ruoyi.base.annotaion.TokenIgnore;
import com.ruoyi.base.controller.BaseController;
import com.ruoyi.base.pojo.HttpResult;
import com.ruoyi.base.utils.inputcheck.InputCheckUtils;
import com.ruoyi.platform.system.user.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户登录
 *
 * @author lcf
 * @version 0.0.1
 * @since 2020-11-13 05:40:22
 */
@Api(tags = {"用户登录"})
@RestController
@Scope("request")
@RequestMapping("/login")
public class LoginController extends BaseController {
    @Autowired
    private LoginService loginService;

    /**
     * 后台登录
     */
    @ApiOperation("后台登录")
    @TokenIgnore
    @PostMapping("/login")
    public HttpResult<String> login(@ApiParam(value = "用户名", required = true) String account,
                                    @ApiParam(value = "密码", required = true) String password,
                                    @ApiParam(value = "验证码key") String verifyKey,
                                    @ApiParam(value = "验证码code") String verifyCode) {
        InputCheckUtils.checkStr(account, "请输入用户名");
        InputCheckUtils.checkStr(password, "请输入密码");

        // 生成令牌
        return data(loginService.login(account, password, verifyKey, verifyCode));
    }

    /**
     * 退出登录
     */
    @ApiOperation("退出登录")
    @TokenIgnore
    @PostMapping("/logout")
    public HttpResult<String> logout() {
        loginService.logout();

        return success();
    }
}